package com.school.base.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

import com.school.base.dao.ITeacherDao;
import com.school.base.model.Teacher;
import com.school.common.base.BaseDao;
import com.school.common.constant.Constant;
import com.school.common.util.MapUtils;
import com.school.common.util.Md5Utils;

public class TeacherDao extends BaseDao implements ITeacherDao {

	@Override
	public Teacher getTeacherById(int id) {
		return (Teacher) this.getObjectById(Teacher.class, id);
	}

	private String getExpressSql(Teacher teacher, List express) {
		String sql = "";
		if (teacher != null) {
			if (teacher.getRealName() != null && !teacher.getRealName().isEmpty()) {
				sql += " and t.real_name like ?";
				express.add("%" + teacher.getRealName() + "%");
			}
			if (teacher.getSubjectId() > 0) {
				sql += " and s.id=?";
				express.add(teacher.getSubjectId());
			}
		}
		return sql;
	}

	@Override
	public List<Teacher> getTeacherList(Teacher teacher, int offset, int pageSize) {
		String sql = "select *,s.name subject from b_teacher t left join b_subject s on s.id=t.subject_id where t.status<>" + Constant.ACCOUNT_STATUS_DELETED;
		List express = new ArrayList();
		sql += this.getExpressSql(teacher, express);
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				query.setParameter(i, express.get(i));
			}
		}
		query.setMaxResults(pageSize);
		query.setFirstResult(offset);
		List list = query.list();
		List<Teacher> teacherList = new ArrayList<Teacher>();
		for (Object obj : list) {
			Teacher t = (Teacher) MapUtils.mapToBean((HashMap<String, Object>) obj, Teacher.class);
			teacherList.add(t);
		}
		return teacherList;
	}

	@Override
	public int getTotalCount(Teacher teacher) {
		String sql = "select count(*) from b_teacher t left join b_subject s on s.id=t.subject_id where t.status<>" + Constant.ACCOUNT_STATUS_DELETED;
		List express = new ArrayList();
		sql += this.getExpressSql(teacher, express);
		return this.getRecordCountBySql(sql, express);
	}

	@Override
	public void updateTeacher(Teacher teacher) {
		String hql = "update Teacher set ";
		List express = new ArrayList();
		if (teacher.getPwd() != null && !teacher.getPwd().isEmpty()) {
			teacher.setPwd(Md5Utils.getMd5(teacher.getPwd()));
			hql += "pwd=?, ";
			express.add(teacher.getPwd());
		}
		hql += "realName=?, ";
		express.add(teacher.getRealName());
		hql += "gender=?, ";
		express.add(teacher.getGender());
		hql += "subjectId=?, ";
		express.add(teacher.getSubjectId());
		hql += "introduction=?, ";
		express.add(teacher.getIntroduction());
		if (teacher.getAvatarSmall() == null || teacher.getAvatarSmall().isEmpty()) {
			teacher.setAvatarSmall(Constant.TEACHER_AVATAR_SMALL_PATH);
		}
		hql += "avatarSmall=?, ";
		express.add(teacher.getAvatarSmall());
		if (teacher.getAvatarLarge() == null || teacher.getAvatarLarge().isEmpty()) {
			teacher.setAvatarLarge(Constant.TEACHER_AVATAR_LARGE_PATH);
		}
		hql += "avatarLarge=?, ";
		express.add(teacher.getAvatarLarge());
		hql += "telPhone=?, ";
		express.add(teacher.getTelPhone());
		hql += "oicqNum=?, ";
		express.add(teacher.getOicqNum());
		hql += "email=?, ";
		express.add(teacher.getEmail());

		// last--------
		hql += "education=? ";
		express.add(teacher.getEducation());

		hql += "where id=?";
		express.add(teacher.getId());
		this.executeHql(hql, express);
	}

	@Override
	public void deleteTeacher(int id) {
		String hql = "update Teacher set status=" + Constant.ACCOUNT_STATUS_DELETED + " where id=" + id;
		this.executeHql(hql);
	}

	@Override
	public void insertTeacher(Teacher teacher) {
		teacher.setPwd(Md5Utils.getMd5(teacher.getPwd()));
		if (teacher.getAvatarSmall() == null || teacher.getAvatarSmall().isEmpty()) {
			teacher.setAvatarSmall(Constant.TEACHER_AVATAR_SMALL_PATH);
		}
		if (teacher.getAvatarLarge() == null || teacher.getAvatarLarge().isEmpty()) {
			teacher.setAvatarLarge(Constant.TEACHER_AVATAR_LARGE_PATH);
		}
		teacher.setStatus(Constant.ACCOUNT_STATUS_INUSED);
		this.insert(teacher);
	}

	@Override
	public boolean checkLoginName(String loginName, int teacherId) {
		List<Criterion> express = new ArrayList<Criterion>();
		express.add(Restrictions.eq("loginName", loginName));
		express.add(Restrictions.ne("id", teacherId));
		List list = this.getList(Teacher.class, express, null);
		if (list != null && !list.isEmpty()) {
			return false;
		}
		return true;
	}

	@Override
	public List<Teacher> getAll() {
		String sql = "select *,s.name subject from b_teacher t left join b_subject s on s.id=t.subject_id where t.status=" + Constant.ACCOUNT_STATUS_INUSED;
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		List list = query.list();
		List<Teacher> teacherList = new ArrayList<Teacher>();
		for (Object obj : list) {
			Teacher t = (Teacher) MapUtils.mapToBean((HashMap<String, Object>) obj, Teacher.class);
			teacherList.add(t);
		}
		return teacherList;
	}

	@Override
	public List<Teacher> getTeacherListByName(String teacherName) {
		List<Criterion> express = new ArrayList<Criterion>();
		express.add(Restrictions.like("realName", "%" + teacherName + "%"));
		return this.getList(Teacher.class, express, null);
	}
}
